/*
 * 
 */
package com.dantfb.services;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.neo4j.shell.util.json.JSONException;
import org.neo4j.shell.util.json.JSONObject;

import com.dantfb.DAO.FriendshipDAO;


 

/**
 * The Class FrienshipService.
 */
public class FriendshipServlet extends HttpServlet{
	
	/*
	 * TODO
	 * 
	 * 		(jsp)Faire un bloc ou on montre les amis, les requetes en attente et les demande de la part des gens qu'on a
	 *		(jsp)Quand on montre un ami on a soit son mur s'il est ami soit un bouton ajouter
	 * 		(dao) faire le add (il est pas fait il est juste commencé)
	 * 
	 * 
	 */
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		HttpSession session = request.getSession(true);
		String id = (String) session.getAttribute("UUID");
		
		//Cas où on ajoute une relation d'amitié
		if(action.equals("add")) {
			String ami = request.getParameter("UUIDAmi");
			FriendshipDAO.addFriend(id, ami);
			response.sendRedirect("home.jsp");
		} else if(action.equals("search")) {
			String searchFriend = request.getParameter("searchFriend");
			response.sendRedirect("search.jsp?searchFriend=" + searchFriend);
		} else if(action.equals("delete")) {
			String uuidAmi = request.getParameter("uuidAmi");
			FriendshipDAO.deleteFriend(id, uuidAmi);
			response.sendRedirect("home.jsp");
		} else if(action.equals("confirm")) {
			String uuidAmi = request.getParameter("uuidAmi");
			FriendshipDAO.confirmFriend(uuidAmi, id);
			response.sendRedirect("home.jsp");
		}
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		HttpSession session = request.getSession(true);
		String id = (String) session.getAttribute("UUID");
		
		//Cas où on ajoute une relation d'amitié
		if(action.equals("add")) {
			String ami = request.getParameter("UUIDAmi");
			FriendshipDAO.addFriend(id, ami);
			response.sendRedirect("home.jsp");
		} else if(action.equals("search")) {
			String searchFriend = request.getParameter("searchFriend");
			response.sendRedirect("search.jsp?searchFriend=" + searchFriend);
		} else if(action.equals("delete")) {
			String uuidAmi = request.getParameter("uuidAmi");
			FriendshipDAO.deleteFriend(id, uuidAmi);
			response.sendRedirect("home.jsp");
		} else if(action.equals("confirm")) {
			String uuidAmi = request.getParameter("uuidAmi");
			FriendshipDAO.confirmFriend(uuidAmi, id);
			response.sendRedirect("home.jsp");
		}
	}
	
}
